<?php

namespace XWAM\Controller;

/**
 * ProductCategoryController 控制器
 * @since 1.0 <2016-5-28> SoChishun <14507247@qq.com> Added.
 */
class ProductCategoryController extends AppbaseController {

    /**
     * 导出数据
     * @since 1.0 <2014-6-13> SoChishun Added.
     */
    public function category_export() {
        $asearch = $this->get_category_search_data();
        $m_category = new \XWAM\Model\ProductCategoryModel();
        $list = $m_category->scope('export')->where($asearch['where'])->select();
        if (!$list) {
            $this->ajaxMsg(false, '找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    public function category_copy($id = 0) {
        if (!$id) {
            $this->ajaxMsg(false, '参数无效');
        }
        $m_category = new \XWAM\Model\ProductCategoryModel();
        $m_category->copy_category($id);
        $this->ajaxMsg(true);
    }

    public function category_list() {
        $search = $_GET;
        $m_category = new \XWAM\Model\ProductCategoryModel();
        $list = $m_category->select_tree(array('where' => $where),array(),$this->site_id);
        $this->assign('tree', $list);
        $this->assign('search', $search);
        $this->display_cpp();
    }

    public function category_edit($id = 0, $pid = 0) {
        $data = array();
        if ($id) {
            $m_category = new \XWAM\Model\ProductCategoryModel();
            $data = $m_category->find($id);
        }
        if (!$data) {
            $data = array('pid' => $pid, 'site_id' => $this->site_id);
        }
        $this->assign('data', $data);
        $this->display();
    }

    public function category_edit_save() {
        $m_category = new \XWAM\Model\ProductCategoryModel();
        $result = $m_category->save_category();
        $this->dialogJump($result['status'], $result['info']);
    }

    public function get_category_tree_json($pid = 0) {
        $options = array();
        if ($pid) {
            $options['where']['pid'] = $pid;
        }
        $m_category = new \XWAM\Model\ProductCategoryModel();
        $data = $m_category->select_json_tree($options);
        $this->ajaxReturn($data);
    }

    function change_status($id, $status) {
        $m_category = new \XWAM\Model\ProductCategoryModel();
        $this->ajaxReturn($m_category->change_status($id, $status));
    }

    function change_sort($id, $sort) {
        $m_category = new \XWAM\Model\ProductCategoryModel();
        $this->ajaxReturn($m_category->change_sort($id, $sort));
    }

    /**
     * category_delete操作
     * @param string $id 主键编号
     * @since 1.0<2015-7-31> SoChishun Added.
     */
    public function category_delete($id = '') {
        $m_category = new \XWAM\Model\ProductCategoryModel();
        $this->ajaxReturn($m_category->delete_category($id));
    }

    // 2016-8-23 SoChishun Added.
    function brand_picker($id, $brand_id = 0) {
        $this->assign('brand_id', $brand_id);
        $this->assign('cid', $id);
        $this->display();
    }

    public function brand_picker_save() {
        $ids = I('id');
        $cid = I('cid');
        if (!$ids || !$cid) {
            $this->error('参数有误!');
        }
        $m_category = new \XWAM\Model\ProductCategoryModel();
        $result = $m_category->where(array('id' => $cid))->setField('brand_id', $ids);
        if (false === $result) {
            $this->error('出错啦：' . $m_category->getDbError());
        } else {
            $this->dialogJump(true);
        }
    }

    function specification_picker($id) {
        $m_category = new \XWAM\Model\ProductCategoryModel();
        $specification_ids = $m_category->where(array('id' => $id))->getField('specification_ids');
        $m_specification = new \XWAM\Model\ProductSpecificationModel();
        $list = $m_specification->where(array('pid' => 0))->order('sort, id desc')->select();
        $this->assign('list', $list);
        $this->assign('specification_ids', $specification_ids);
        $this->assign('cid', $id);
        $this->display();
    }

    public function specification_picker_save() {
        $ids = I('id');
        $cid = I('cid');
        if (!$ids || !$cid) {
            $this->error('参数有误!');
        }
        $m_category = new \XWAM\Model\ProductCategoryModel();
        $result = $m_category->where(array('id' => $cid))->setField('specification_ids', implode(',', $ids));
        if (false === $result) {
            $this->error('出错啦：' . $m_category->getDbError());
        } else {
            $this->dialogJump(true);
        }
    }

}
